import 'package:flutter/cupertino.dart';
import 'package:flutter_shader/generated/assets.dart';
import 'package:flutter_shader/utils/game_loop.dart';
import 'package:flutter_shader/widgets/play_button.dart';
import 'package:flutter_shader/widgets/shadertoy.dart';

const _padding = EdgeInsets.all(10);

/// Created by changlei on 2024/2/22.
///
/// siri
class SiriPage extends StatefulWidget {
  /// siri
  const SiriPage({super.key});

  @override
  State<SiriPage> createState() => _SiriPageState();
}

class _SiriPageState extends State<SiriPage> {
  late final _loop = GameLoop();

  static const _fragments = [
    _Siri.new,
    _Mario.new,
  ];

  bool _isLooping = true;

  @override
  void initState() {
    super.initState();
    _loop.start();
  }

  @override
  void dispose() {
    _loop.stop();
    _loop.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        trailing: PlayButton(
          isPlaying: _isLooping,
          onPressed: () {
            if (_isLooping) {
              _loop.stop();
            } else {
              _loop.start();
            }
            setState(() {
              _isLooping = !_isLooping;
            });
          },
        ),
      ),
      child: Builder(
        builder: (context) {
          final viewPadding = MediaQuery.viewPaddingOf(context);
          return ListView.separated(
            padding: viewPadding + _padding,
            itemCount: _fragments.length,
            itemBuilder: (context, index) {
              return _fragments[index].call(
                loop: _loop,
              );
            },
            separatorBuilder: (context, index) {
              return const SizedBox(
                height: 16,
              );
            },
          );
        },
      ),
    );
  }
}

class _Siri extends StatelessWidget {
  const _Siri({
    Key? key,
    required this.loop,
  }) : super(key: key);

  final GameLoop loop;

  @override
  Widget build(BuildContext context) {
    return Shadertoy(
      assetKey: Assets.shaders(Shaders.siri),
      loop: loop,
      builder: (context) {
        return AnimatedContainer(
          duration: const Duration(milliseconds: 600),
          curve: Curves.fastOutSlowIn,
          width: 100,
          height: 100,
          decoration: const ShapeDecoration(
            color: Color(0xFF000000),
            shape: CircleBorder(),
          ),
        );
      },
    );
  }
}

class _Mario extends StatelessWidget {
  const _Mario({
    Key? key,
    required this.loop,
  }) : super(key: key);

  final GameLoop loop;

  @override
  Widget build(BuildContext context) {
    return Shadertoy(
      assetKey: Assets.shaders(Shaders.mario),
      loop: loop,
      builder: (context) {
        return AnimatedContainer(
          duration: const Duration(milliseconds: 600),
          curve: Curves.fastOutSlowIn,
          width: 400,
          height: 200,
          decoration: const ShapeDecoration(
            color: Color(0xFF000000),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(12)),
            ),
          ),
        );
      },
    );
  }
}
